<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\admin\model\Team;
use app\common\constant\Constant;
use app\common\model\Authentication;
use app\common\model\CaptainApplication;
use app\common\model\User;
use app\common\model\Withdrawrecord;
use think\Db;
use think\Exception;

class WithdrawManager extends BaseManager
{

    /**
     * 获取当前用户的提现记录
     * @param User $user
     * @param $status
     * @param $type
     * @param $page
     * @param $step
     * @return array
     */
    public static function withdrawList(User $user,$status=null,$type='wechat',$page=1,$step=Constant::PAGE_LIMIT):array
    {
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        try {
            if ($page < 1){
                $page = 1;
            }
            $start = ($page - 1) * $step;
            $withdrawModel = new Withdrawrecord();
            $count = $withdrawModel->where('user_id',$user->id)
                ->where(function ($query) use($status){
                    if (!is_null($status)){
                        return $query->where('status',$status);
                    }
                })
                ->where(function ($query) use($type){
                    if ($type){
                        return $query->where('type',$type);
                    }
                })
                ->count();

            $info = $withdrawModel->where('user_id',$user->id)
                ->where(function ($query) use($status){
                    if (!is_null($status)){
                        return $query->where('status',$status);
                    }
                })
                ->where(function ($query) use($type){
                    if ($type){
                        return $query->where('type',$type);
                    }
                })
                ->limit($start,$step)
                ->select();

            $return['list'] = $info;
            $pageData = page($count,$step,$page);
            $data = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($data);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }
}